<?php 
    include "../service/util/authenticateSession.php";
	
	include ("messages.properties.php"); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
    include_once("../service/CategoryService.php");
    include_once("../service/ProductService.php");
    
    if(isset($_GET['do'])){
        $action = $_GET['do'];
        if($action == "view"){
            $pro_id = $_GET["n"];
            $productInfor = mysql_fetch_array(getProduct($pro_id));
        }
    }
    else{
        echo "<script>location.href='product.php';</script>";
    }
?>
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
	<title><?php echo $action=="view"?"Sản phẩm: ".$productInfor["PRO_NAME_EN"]."-".$productInfor["PRO_NAME_VN"]:"Thêm sản phẩm mới"; ?></title>
    <?php
    include("import.php");
    ?>
	<link rel="stylesheet" href="../stylesheet/admin-style.css" type="text/css" media="all" />
    <style>
    #table tr td img { padding-top: 12px !important; margin-bottom: 0 !important;}
    #table tr td { padding-bottom: 0 !important; } 
    </style>
</head>
<body>
<?php
include ("header.php");
?>

<!-- Container -->
<div id="container">
	<div class="shell">
		<div class="small-nav">
			<a href="product.php">Sản phẩm</a>
			<span>&gt;</span>
			<?php echo $action=="view"?$productInfor["PRO_NAME_EN"]."-".$productInfor["PRO_NAME_VN"]:"Thêm sản phẩm mới"; ?>
		</div>
       
		<!-- End Small Nav -->
		
        <?php
        	$message = "";
			$class = "";
			if (isset($_GET['msg'])) $msg = $_GET['msg'];
			
			if(isset($msg)){
				if($msg == "2"){
                    $message = $messages["newProductFail"];
					$class = "msg msg-error"; 
				}else if($msg == "3"){
                    $message = $messages["deleteProductImageOk"];
					$class = "msg msg-ok"; 
				}else if($msg == "4"){
                    $message = $messages["deleteProductImageFail"];
					$class = "msg msg-error"; 
				}else if($msg == "5"){
                    $message = $messages["updateProductOk"];
					$class = "msg msg-ok"; 
				}else if($msg == "6"){
                    $message = $messages["updateProductFail"];
					$class = "msg msg-error"; 
				}
                
			}
			if ($message == "") $class = 'msg msg-hidden';
		?>
        <!--Message-->
		<div class="<?php echo $class; ?>" id="msg">
            <p><strong><?php echo $message; ?></strong></p>
            <a href="#" onclick="hideMessage();" class="close">close</a>
        </div>
		<br />
		<!-- Main -->
		<div id="main">
			<div class="cl">&nbsp;</div>
			
			<!-- Content -->
			<div id="content">        
				<!-- Box -->
				<div class="box">
					<!-- Box Head -->
					<div class="box-head">
						<h2>Thêm Sản Phẩm</h2>
					</div>
					<!-- End Box Head -->
					<form action="<?php echo $action == "view"?"../action/UpdateProductInformation.php?n=".$pro_id:"../action/AddProductAction.php"; ?>" method="post" enctype="multipart/form-data">
						<!-- Form -->
						<div class="form">
								<p>
									<label>Tên Sản Phẩm (Tiếng Anh)</label>
									<input name="pro_name_en" type="text" class="field size1" value="<?php echo $action=="view"?$productInfor["PRO_NAME_EN"]:""; ?>"/>
								</p>
                                <p>
									<label>Tên Sản Phẩm (Tiếng Việt)</label>
									<input name="pro_name_vn" type="text" class="field size1" value="<?php echo $action=="view"?$productInfor["PRO_NAME_VN"]:""; ?>"/>
								</p>
                                <p>
                                	<label>Mã Sản Phẩm</label>
                                    <input name="pro_code" type="text" class="field size1" value="<?php echo $action=="view"?$productInfor["PRO_CODE"]:""; ?>"/>
                                </p>	
                                <p>
                                	<label>Giá tiền (VD: 1000000)</label>
                                    <input name="pro_price" type="text" class="field size1" value="<?php echo $action=="view"?round($productInfor["PRO_PRICE"]):""; ?>"/>
                                </p>
                                <p>
                                	<label>Trạng thái</label>
                                    <select class="field size1" name="pro_status">
                                        <?php
                                        //Display Status
                                        if($action == "view"){
                                            for ($i = 1; $i <= 4; $i++){
                                                if($i == 1){
                                                    echo "<option value='1'";
                                                    //select or not
                                                    echo $productInfor["PRO_STATUS"] == $i?" selected='selected' ":"";
                                                    echo ">Còn hàng</option>";
                                                }else if($i == 2){
                                                    echo "<option value='2'";
                                                    //select or not
                                                    echo $productInfor["PRO_STATUS"] == $i?" selected='selected' ":"";
                                                    echo ">Tạm hết hàng</option>";
                                                }else if($i == 3){
                                                    echo "<option value='3'";
                                                    //select or not
                                                    echo $productInfor["PRO_STATUS"] == $i?" selected='selected' ":"";
                                                    echo ">Ngừng bán</option>";
                                                }else if($i == 4){
                                                    echo "<option value='4'";
                                                    //select or not
                                                    echo $productInfor["PRO_STATUS"] == $i?" selected='selected' ":"";
                                                    echo ">Sắp về</option>";
                                                }
                                            }
                                        }else{
                                            echo "<option value='1'>Còn hàng</option>";
                                            echo "<option value='2'>Tạm hết hàng</option>";
                                            echo "<option value='3'>Ngừng bán</option>";
                                            echo "<option value='4'>Sắp về</option>";
                                        }
                                        ?>
									</select>
                                </p>
                                <p>
                                	<label>Thuộc loại</label>
                                    <?php
                                            //$categoryInfor = mysql_fetch_array(getCategoryOfProduct($pro_id));
                                            
                                            function showAllCategories($cat_par_id, $selected_cat_par_id){
                                                $categories = getAllChildrenCategoryOf($cat_par_id);
                                                while($aCate = mysql_fetch_array($categories)){
                                                        if($selected_cat_par_id == $aCate["CAT_ID"]){
                                                            echo "<option value='".$aCate["CAT_ID"]."' selected='selected'>".$aCate["CAT_NAME_EN"]."-".$aCate["CAT_NAME_VN"]."</option>";
                                                        }else{
                                                            echo "<option value='".$aCate["CAT_ID"]."'>".$aCate["CAT_NAME_EN"]."-".$aCate["CAT_NAME_VN"]."</option>";
                                                        }
                                                        
                                                        if(hasChild($aCate["CAT_ID"])){
                                                            showAllCategories($aCate['CAT_ID'], $selected_cat_par_id);
                                                        }
                                                }
                                            }
                                    ?>
                                    <select class="field size1" name="cat_parent_id">
                                        <?php
                                        if($action == "view"){
                                            showAllCategories(0, $productInfor["CAT_ID"]);    
                                        }else{
                                            showAllCategories(0, -1);
                                        }
                                        
                                        ?>
									</select>
                                </p>
                                <p>
									<label>Miêu tả (Tiếng Anh)</label>
									<textarea name="pro_description_en" class="field size1" rows="10" cols="30"><?php echo $action=="view"?$productInfor["PRO_DESCRIPTION_EN"]:"";?></textarea>
								</p>
                                <p>
									<label>Miêu tả (Tiếng Việt)</label>
									<textarea name="pro_description_vn" class="field size1" rows="10" cols="30"><?php echo $action=="view"?$productInfor["PRO_DESCRIPTION_VN"]:"";?></textarea>
								</p>
                                <p>
                                	<label>Hình ảnh</label>
                                    <table id="table">
                                        <?php
                                            $url1 = "";
                                            $url2 = "";
                                            $url3 = "";
                                            $url4 = "";
                                            $imgid1 = 0;
                                            $imgid2 = 0;
                                            $imgid3 = 0;
                                            $imgid4 = 0;
                                            if($action == "view"){
                                                $images = getImageOfProduct($pro_id);
                                                $numbImg = mysql_num_rows($images);
                                                
                                                for($i = 1; $i <= $numbImg; $i++){
                                                    $row = mysql_fetch_array($images);
                                                    if($i==1){
                                                        $url1 = $row["IMG_PATH"];
                                                        $imgid1 = $row["IMG_ID"];
                                                    }else if($i == 2){
                                                        $url2 = $row["IMG_PATH"];
                                                        $imgid2 = $row["IMG_ID"];
                                                    }else if($i == 3){
                                                        $url3 = $row["IMG_PATH"];
                                                        $imgid3 = $row["IMG_ID"];
                                                    }else if($i == 4){
                                                        $url4 = $row["IMG_PATH"];
                                                        $imgid4 = $row["IMG_ID"];
                                                    }
                                                }    
                                                //echo $imgid1."-".$imgid2."-".$imgid3."-".$imgid4;
                                            }
                                        ?>
                                    	<tr>
                                        	<td style="vertical-align: bottom;">
                                            Ảnh 1 (anh chinh):
                                            </td>
                                        	<td style="vertical-align: bottom;">
                                            <input name="img1" type="file"/>
                                            </td>
                                            <td>
                                            <?php 
                                            if($action == "view"){
                                                if($url1 != ""){
                                                    echo "<img width='60px' height='auto' src='../images/product/PRO_".$pro_id."/".$url1."'/>";
                                                    echo "<a href='../action/DeleteProductImageAction.php?n=".$imgid1."&p=".$pro_id."'>X</a>";    
                                                }else{
                                                    echo "<img width='60px' height='auto' src='../images/cat/noimg.jpg'/>";    
                                                }
                                            }
                                            ?>
                                            </td>
                                        </tr>
                                        
                                        <tr>
                                        	<td style="vertical-align: bottom;">
                                            Ảnh 2:
                                            </td>
                                        	<td style="vertical-align: bottom;">
                                            <input name="img2" type="file"/>
                                            </td>
                                            <td>
                                            <?php 
                                            if($action == "view"){
                                                if($url2 != ""){
                                                    echo "<img width='60px' height='auto' src='../images/product/PRO_".$pro_id."/".$url2."'/>";
                                                    echo "<a href='../action/DeleteProductImageAction.php?n=".$imgid2."&p=".$pro_id."'>X</a>";    
                                                }else{
                                                    echo "<img width='60px' height='auto' src='../images/cat/noimg.jpg'/>";    
                                                }
                                            }
                                            ?>
                                            </td>
                                        </tr>
                                        
                                        <tr>
                                        	<td style="vertical-align: bottom;">
                                            Ảnh 3:
                                            </td>
                                        	<td style="vertical-align: bottom;">
                                            <input name="img3" type="file"/>
                                            </td>
                                            <td>
                                            <?php 
                                            if($action == "view"){
                                                if($url3 != ""){
                                                    echo "<img width='60px' height='auto' src='../images/product/PRO_".$pro_id."/".$url3."'/>";
                                                    echo "<a href='../action/DeleteProductImageAction.php?n=".$imgid3."&p=".$pro_id."'>X</a>";    
                                                }else{
                                                    echo "<img width='60px' height='auto' src='../images/cat/noimg.jpg'/>";    
                                                }
                                            }
                                            ?>
                                            </td>
                                        </tr>
                                        
                                        <tr>
                                        	<td style="vertical-align: bottom;">
                                            Ảnh 4:
                                            </td>
                                        	<td style="vertical-align: bottom;">
                                            <input name="img4" type="file"/>
                                            </td>
                                            <td>
                                            <?php 
                                            if($action == "view"){
                                                if($url4 != ""){
                                                    echo "<img width='60px' height='auto' src='../images/product/PRO_".$pro_id."/".$url4."'/>";
                                                    echo "<a href='../action/DeleteProductImageAction.php?n=".$imgid4."&p=".$pro_id."'>X</a>";    
                                                }else{
                                                    echo "<img width='60px' height='auto' src='../images/cat/noimg.jpg'/>";    
                                                }
                                            }
                                            ?>
                                            </td>
                                        </tr>
                                    </table>
                                </p>
						</div>
						<!-- End Form -->
						
						<!-- Form Buttons -->
						<div class="buttons">
							<input type="submit" class="button" value="<?php echo $action == "view"?"Cập nhật":"Thêm"; ?>" />
                            <input type="reset" class="button" value="Xoa Bieu Nhap" />
                            <input type="button" class="button" value="Trở Về" onclick="window.location = 'product.php'" />
						</div>
						<!-- End Form Buttons -->
					</form>
				</div>
				<!-- End Box -->

			</div>
			<!-- End Content -->
			

			
			<div class="cl">&nbsp;</div>			
		</div>
		<!-- Main -->
	</div>
</div>
<!-- End Container -->

<?php
	include "footer.php";
	ConnectionManager::getInstance()->closeConnection();
?>		
</body>
</html>
